package fr.grobim.hangovers.gestionBudget.actions.factures;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;

import fr.grobim.hangovers.gestionBudget.services.factures.FacturationService;
import fr.grobim.hangovers.gestionBudget.actions.enums.AttributsSession;
import fr.grobim.hangovers.gestionBudget.actions.support.AbstractActionSupport;
import fr.grobim.hangovers.gestionBudget.beans.factures.SourceFacture;

@Namespace(value = "/")
@ResultPath(value = "/")
public class GestionSourcesFactureAction extends AbstractActionSupport {

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private FacturationService facturationService;
	
	private Integer idSourceFactureSelectionnee;
	
	private SourceFacture sourceFactureAjout;
	
	private SourceFacture sourceFactureModif;
	
	@Action(value = "ajouterSourceFacture", results = {
			@Result(location = "facture.gestionSources.lister", type = "tiles"),
			@Result(name = INPUT, location = "facture.gestionSources.lister", type = "tiles")})
	public String ajouterSourceFacture() throws Exception {
		String retourAction = SUCCESS;
		sourceFactureAjout.setLibelle(Character.toUpperCase(sourceFactureAjout.getLibelle().charAt(0)) + sourceFactureAjout.getLibelle().substring(1));
		try {
			facturationService.ajouterOuModifierSourceFacture(sourceFactureAjout);
			sourceFactureAjout.setLibelle(StringUtils.EMPTY);
			retirerObjetDeSession(AttributsSession.GESTION_SOURCE_FACTURE_LISTE);
			addActionMessage(getText("facture.sourceFacture.ajouterSource.confirmation"));
		} catch (ConstraintViolationException e) {
			e.printStackTrace();
			retourAction = INPUT;
			addActionError(getText("facture.sourceFacture.ajouterSource.erreur.nonUnique"));
		}
		
		return retourAction;
	}
	
	@Action(value = "afficherDetailSourceFacture", results = {
			@Result(location = "facture.gestionSources.lister", type = "tiles")})
	public String afficherDetailSourceFacture() throws Exception {
		sourceFactureModif = facturationService.obtenirSourceFacture(idSourceFactureSelectionnee);
		return SUCCESS;
	}
	
	@Action(value = "modifierSourceFacture", results = {
			@Result(location = "facture.gestionSources.lister", type = "tiles"),
			@Result(name = INPUT, location = "facture.gestionSources.lister", type = "tiles")})
	public String modifierSourceFacture() throws Exception {
		String retourAction = SUCCESS;
		sourceFactureModif.setLibelle(Character.toUpperCase(sourceFactureModif.getLibelle().charAt(0)) + sourceFactureModif.getLibelle().substring(1));
		sourceFactureModif.setId(idSourceFactureSelectionnee);
		try {
			SourceFacture sourceOrigine = facturationService.obtenirSourceFacture(idSourceFactureSelectionnee);
			if (!StringUtils.equalsIgnoreCase(sourceOrigine.getLibelle(),
												sourceFactureModif.getLibelle())) {
				facturationService.ajouterOuModifierSourceFacture(sourceFactureModif);
				sourceFactureModif.setLibelle(StringUtils.EMPTY);
				idSourceFactureSelectionnee = null;
				retirerObjetDeSession(AttributsSession.GESTION_SOURCE_FACTURE_LISTE);
				addActionMessage(getText("facture.sourceFacture.modifierSource.confirmation"));
			} else {
				addActionError(getText("facture.sourceFacture.modifierSource.erreur.pasDeModification"));
				retourAction = INPUT;
			}
		} catch (ConstraintViolationException e) {
			e.printStackTrace();
			retourAction = INPUT;
			addActionError(getText("facture.sourceFacture.ajouterSource.erreur.nonUnique"));
		}
		return retourAction;
	}
	
	@Action(value = "supprimerSourceFacture", results = {
			@Result(location = "facture.gestionSources.lister", type = "tiles")})
	public String supprimerSourceFacture() throws Exception {
		try {
			facturationService.supprimerSourceFacture(idSourceFactureSelectionnee);
			idSourceFactureSelectionnee = null;
			addActionMessage(getText("facture.sourceFacture.supprimerFacture.confirmation"));
			retirerObjetDeSession(AttributsSession.GESTION_SOURCE_FACTURE_LISTE);
		} catch (DataIntegrityViolationException e) {
			addActionError(getText("facture.sourceFacture.modifierFacture.erreur.reglementsPresents"));
			sourceFactureModif = facturationService.obtenirSourceFacture(idSourceFactureSelectionnee);
		}
		return SUCCESS;
	}

	public Integer getIdSourceFactureSelectionnee() {
		return idSourceFactureSelectionnee;
	}

	public void setIdSourceFactureSelectionnee(Integer idSourceFactureSelectionnee) {
		this.idSourceFactureSelectionnee = idSourceFactureSelectionnee;
	}

	public SourceFacture getSourceFactureAjout() {
		return sourceFactureAjout;
	}

	public void setSourceFactureAjout(SourceFacture sourceFactureAjout) {
		this.sourceFactureAjout = sourceFactureAjout;
	}

	public SourceFacture getSourceFactureModif() {
		return sourceFactureModif;
	}

	public void setSourceFactureModif(SourceFacture sourceFactureModif) {
		this.sourceFactureModif = sourceFactureModif;
	}

}
